package cn.hahah.module.blog.service.userhistorybrowse;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.userhistorybrowse.vo.*;
import cn.hahah.module.blog.dal.dataobject.userhistorybrowse.UserHistoryBrowseDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.userhistorybrowse.UserHistoryBrowseMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户文章浏览记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserHistoryBrowseServiceImpl implements UserHistoryBrowseService {

    @Resource
    private UserHistoryBrowseMapper userHistoryBrowseMapper;

    @Override
    public Long createUserHistoryBrowse(UserHistoryBrowseSaveReqVO createReqVO) {
        // 插入
        UserHistoryBrowseDO userHistoryBrowse = BeanUtils.toBean(createReqVO, UserHistoryBrowseDO.class);
        userHistoryBrowseMapper.insert(userHistoryBrowse);
        // 返回
        return userHistoryBrowse.getId();
    }

    @Override
    public void updateUserHistoryBrowse(UserHistoryBrowseSaveReqVO updateReqVO) {
        // 校验存在
        validateUserHistoryBrowseExists(updateReqVO.getId());
        // 更新
        UserHistoryBrowseDO updateObj = BeanUtils.toBean(updateReqVO, UserHistoryBrowseDO.class);
        userHistoryBrowseMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserHistoryBrowse(Long id) {
        // 校验存在
        validateUserHistoryBrowseExists(id);
        // 删除
        userHistoryBrowseMapper.deleteById(id);
    }

    private void validateUserHistoryBrowseExists(Long id) {
        if (userHistoryBrowseMapper.selectById(id) == null) {
            throw exception(USER_HISTORY_BROWSE_NOT_EXISTS);
        }
    }

    @Override
    public UserHistoryBrowseDO getUserHistoryBrowse(Long id) {
        return userHistoryBrowseMapper.selectById(id);
    }

    @Override
    public PageResult<UserHistoryBrowseDO> getUserHistoryBrowsePage(UserHistoryBrowsePageReqVO pageReqVO) {
        return userHistoryBrowseMapper.selectPage(pageReqVO);
    }

}